package 信使;

import java.util.*;

public class Main {
    static int N = 110, n, m;
    static int[][] dist = new int[N][N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        for (int i = 0; i <= n; i++) {
            Arrays.fill(dist[i], 0x3f3f3f3f);
        }
        for (int i = 1; i <= n; i++) {
            dist[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            dist[a][b] = dist[b][a] = Math.min(dist[a][b], c);
        }

        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
        int res = 0;
        for (int i = 1; i <= n; i++) {
            if (dist[1][i] == 0x3f3f3f3f) {
                res = -1;
                break;
            } else {
                res = Math.max(res, dist[1][i]);
            }
        }
        System.out.println(res);
    }
}
